﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace xna_game_engine.source
{
    public class SpellManager
    {
        // Members
        private List<BaseSpell> mActiveSpells;


        // Functions
        public SpellManager()
        {
            mActiveSpells = new List<BaseSpell>();
        }

        // TODO: Overload for random placement
        public void AddSpell(Vector2 Pos, BaseSpell spell)
        {
            mActiveSpells.Add(spell);
            spell.Use();
        }

        public void Update(GameTime gt)
        {
            for (int i = 0; i < mActiveSpells.Count; i++)
            {
                mActiveSpells[i].Update(gt);
                if (!mActiveSpells[i].Active)
                {
                    mActiveSpells.RemoveAt(i);
                }
            }

        }

        public void Draw()
        {
            foreach (BaseSpell spell in mActiveSpells)
            {
                spell.Draw();
            }
        }

        public void CheckCollisions(BaseWizard player)
        {
            for (int i = 0; i < mActiveSpells.Count; i++)
            {
                if (mActiveSpells[i].Bounds.Intersects(player.Bounds) && player != mActiveSpells[i].Owner
                    && mActiveSpells[i].Type == BaseSpell.SPELL_TYPE.DAMAGE)
                {
                    if (!player.Reflective)
                    {
                        player.ModHealth(-(mActiveSpells[i].Damage), Color.Red);
                    }
                    else
                    {
                        mActiveSpells[i].Owner.ModHealth(-(mActiveSpells[i].Damage), Color.Green);
                    }
                    mActiveSpells.RemoveAt(i);
                }
                //else
                //{
                //    for (int j = 0; j < mActiveSpells.Count; j++)
                //    {
                //        if (j == i)
                //        {
                //            continue;
                //        }
                //        else if (mActiveSpells[i].Bounds.Intersects(mActiveSpells[j].Bounds))
                //        {
                //            if (mActiveSpells[j].Type == BaseSpell.SPELL_TYPE.REFLECT &&
                //                mActiveSpells[i].Type != BaseSpell.SPELL_TYPE.REFLECT)
                //            {
                //                if (mActiveSpells[i].Type == BaseSpell.SPELL_TYPE.DAMAGE)
                //                {
                //                    mActiveSpells[i].Owner.ModHealth(mActiveSpells[i].Damage, Color.Green);
                //                }
                //            }
                //        }
                //    }
                //}
            }
        }
    }
}
